package com.xxl.job.executor.service.jobhandler;

import com.ruoyi.bizsys.domain.CustBankCard;
import com.ruoyi.bizsys.mapper.CustBankCardMapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class BankCardStateJob {

    @Autowired
    CustBankCardMapper custBankCardMapper;

    @XxlJob("bankCardStateJobHandler")
    public void execute() {
        XxlJobHelper.log("银行卡时效性任务(每月) Start >>>>>>>>>>");

        //查询绑卡记录中的绑卡失效记录
        List<CustBankCard> custBankCards = custBankCardMapper.selectBindCardPeriod();
        if (CollectionUtils.isEmpty(custBankCards)) {
            XxlJobHelper.log("没有待处理的银行卡数据，任务结束。");
            XxlJobHelper.log("银行卡时效性任务(每月) End >>>>>>>>>>");
            return;
        }

        XxlJobHelper.log("待处理的银行卡数量为：{}", custBankCards.size());

        //取出list集合修改卡片状态
        List<CustBankCard> list = new ArrayList<>();
        for (int i = 0; i < custBankCards.size(); i++) {
            //设置失效状态为0
            CustBankCard custBankCard = custBankCards.get(i);
            custBankCard.setCardStatus("0");
            list.add(custBankCard);

            //list 集合 分批执行入库
            if (list.size() % 1000 == 0 || i == custBankCards.size() - 1) {
                XxlJobHelper.log("执行批量更新");
                try {
                    int result = custBankCardMapper.updateBatch(custBankCards);
                    XxlJobHelper.log("----- updateBatch bankCardJob : " + result);
                } catch (Exception e) {
                    XxlJobHelper.log("批量更新异常");
                    XxlJobHelper.log(e);
                }
            }
        }

        XxlJobHelper.log("银行卡时效性任务(每月) End >>>>>>>>>>");
    }
}